---
category: Array
contributors:
    - nico2che
created: '2020-05-01'
title: Flatten an array
updated: '2021-10-13'
---

**JavaScript version**

```js flat.js
const flat = (arr) =>
    [].concat.apply(
        [],
        arr.map((a) => (Array.isArray(a) ? flat(a) : a))
    );

// Or
const flat = (arr) => arr.reduce((a, b) => (Array.isArray(b) ? [...a, ...flat(b)] : [...a, b]), []);

// Or
// See the browser compatibility at https://caniuse.com/#feat=array-flat
const flat = (arr) => arr.flat();
```

**Example**

```js example.js
flat(['cat', ['lion', 'tiger']]); // ['cat', 'lion', 'tiger']
```
